home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / jed096_1.zip / SLANG / SRC / SLASYNC.C < prev    next >
C/C++ Source or Header  |  1994-04-26  |  2KB  |  131 lines

  1. /* Asynchronous processes using ptys */
  2. #ifdef HAS_SUBPROCESSES
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/time.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10.  
  11. #include <unistd.h>
  12.  
  13. #include "slang.h"
  14. #include "_slang.h"
  15. #include "slfile.h"
  16.  
  17. /* On ultrix, pseudo terminals are named /dev/tty?? and /dev/pty??
  18.    where ?? is [pq][0-9a-f] */
  19.       
  20. static char PTY_Name[80], TTY_Name[80];
  21.       
  22. int open_pty ()
  23. {
  24.    int ft, fp;
  25.    char *p, *t, *p1, *t1;
  26.    char *list1 = "pq", *list2 = "0123456789abcdef";
  27.    
  28.    strcpy (PTY_Name, "/dev/pty");
  29.    strcpy (TTY_Name, "/dev/tty");
  30.    
  31.    p = PTY_Name + strlen (PTY_Name); t = TTY_Name + strlen (TTY_Name);
  32.  
  33.    for (p1 = list1; *p1 != 0; p1++)
  34.      {
  35.     *p++ = *p1;  *t++ = *p1;
  36.     for (t1 = list2; *t1 != 0; t1++)
  37.       {
  38.          *p++ = *t1; *p = 0;
  39.          *t++ = *t1; *t = 0;
  40.          
  41.          if ((fp = open (PTY_Name, O_RDWR)) >= 0)
  42.            {
  43.           if ((ft = open (TTY_Name, O_RDWR)) >= 0)
  44.             {
  45.                close (ft);
  46.                return fp;
  47.             }
  48.           close (fp);
  49.            }
  50.          t--; p--;
  51.       }
  52.     t--; p--;
  53.      }
  54.    
  55.    return -1;
  56. }
  57.  
  58.          
  59.    
  60.  
  61. int child_code (char *name)
  62. {
  63.    execl (name, NULL);
  64.    exit(0);
  65. }
  66.  
  67. /* returns the child pid upon success or -1 upon error.  */
  68. static int create_process (char *name, int *fd)
  69. {
  70.    int child_pid;
  71.    int ft;
  72.    
  73.    *fd = open_pty();
  74.    if (*fd < 0) return -1;
  75.    
  76.    
  77.    if (fcntl (*fd, F_SETFL, O_NDELAY) < 0)
  78.      {
  79.     fprintf(stderr, "fcntl 1 failed.\n");
  80.      }
  81.  
  82.       
  83.    if (0 == (child_pid = fork()))
  84.      {
  85.     setsid ();
  86.     if ((ft = open (TTY_Name, O_RDWR)) < 0)
  87.       {
  88.          fprintf(stderr, "Child: unable to open tty");
  89.       }
  90.     dup2(ft, fileno(stderr));
  91.     dup2(ft, fileno(stdout));
  92.     dup2(ft, fileno(stdin));
  93.     child_code (name);
  94.      }
  95.    
  96.    return child_pid;
  97. }
  98.  
  99. void child_signal(int sig)
  100. {
  101. }
  102.  
  103.  
  104. int SLcreate_child_process (char *name)
  105. {
  106.    SL_File_Table_Type *t;
  107.    FILE *fp;
  108.    int fd, pid;
  109.    
  110.    if ((t = get_file_table_entry()) == NULL) return (-1);
  111.    
  112.    if (-1 == (pid = create_process (name, &fd))) return -1;
  113.      
  114.    signal (SIGCHLD, child_signal);
  115.    
  116.    if ((fp = fdopen (fd, "r+")) == NULL)
  117.      {
  118.     close (fd);
  119.     return -1;
  120.      }
  121.    t->fp = fp;
  122.    t->fd = fd;
  123.    t->flags = SL_WRITE | SL_READ | SL_PROCESS;
  124.    t->pid = pid;
  125.    SLang_push_integer (pid);
  126.    return ((int) (t - SL_File_Table));
  127. }
  128.    
  129.    
  130. #endif  /* HAS_SUBPROCESSES */
  131.